import { fetch, coordFormat, alert, confirm } from './util'

// 获取商店列表
export function getSellers(options) {
  return fetch({
    url: `s/v1/${options.siteId}/stores`,
    data: options
  })
}

// 获取当前经纬度获取所属站点
export function getAddressSite(options) {
  return fetch({
    url: 'site/v1/site',
    data: options
  })
}
// 获取当前经纬度站点种类
export function getSellerType(options) {
  let pid = -1
  return fetch({
    url: `st/v1/${options.siteId}/${pid}/storeTypes`
  })
}

// 获取商店详情
export function getSellerInfo(options) {
  var { id } = options

  return fetch({
    url: `s/v1/store/${id}`
  })
}

// 获取商品分类
export function getProductTypes(options) {
  var { id } = options

  return fetch({
    url: `pt/v1/${id}/productTypes`
  })
}

// 获取商品列表
export function getProductsInfo(options) {
  var { storeId, productTypeId } = options

  return fetch({
    url: `p/v1/${storeId}/${productTypeId}/products`
  })
}

// 获取商店评论
export function getReviews(options) {
  var { id, page, size } = options

  return fetch({
    url: `c/v1/store/${id}/comments?page=${page}&size=${size}`
  })
}

// 短信验证码
export function getCode(options) {
  const { phone } = options
  return fetch({
    url: 'pc/v1/send',
    method: 'POST',
    data: {
      telephone: phone,
      type: 1
    }
  })
}

// 登录
export function login(options) {
  const { phone, code, openId, avatar, nickname, attach } = options
  return fetch({
    url: 'u/v1/login',
    method: 'POST',
    data: {
      telephone: phone,
      code,
      openId: openId,
      avatar,
      nickname,
      attach
    }
  })
}
// 学号登录
export function learnNumLogin(options) {
  const { learnNum, password, openId, avatar, nickname, attach } = options
  return fetch({
    url: 'u/v1/learnNumLogin',
    method: 'POST',
    data: {
      learnNum,
      password,
      openId,
      avatar,
      nickname,
      attach
    }
  })
}
// 退出账号
export function logout(options) {
  const { phone, success, error } = options
  fetch({
    url: 'index.php?m=Api&c=WeixinMall&a=logout',
    data: {
      phone
    }
    // success,
    // error
  })
}

// 获取登录信息
export function getLoginInfo(options) {
  const { success, error } = options
  wx.login({
    success(res) {
      fetch({
        url: 'index.php?m=Api&c=WeixinMall&a=getLoginInfo',
        data: {
          wx_code: res['code'],
          session_3rd: wx.getStorageSync('session_3rd')
        },
        success,
        error
      })
    },
    error(res) {
      alert(res['errMsg'])
      error && error(res['errMsg'])
    }
  })
}

// 获取用户地址列表
export function getUserAddrs(options) {
  return fetch({ url: 'a/v1/address' })
}

// 设置勾选地址
export function setDefaultAddr(options) {
  const { id, type } = options
  return fetch({
    url: `a/v1/address/default/${id}`,
    method: 'PUT',
    data: {
      type
    }
  })
}

// 获取用户地址详情
export function getUserAddr(options) {
  const { addr_id } = options
  return fetch({
    url: `a/v1/address/${addr_id}`
  })
}

// 新增用户地址
export function addUserAddr(options) {
  if (options.id) {
    return updateUserAddr(options)
  }
  const { type, phone, areaName, addressDetail, nickname, longitude, latitude } = options
  return fetch({
    url: 'a/v1/address',
    method: 'POST',
    data: {
      type,
      areaName,
      phone,
      addressDetail,
      nickname,
      longitude,
      latitude
    }
  })
}

// 修改地址
export function updateUserAddr(options) {
  const {
    id,
    type,
    phone,
    areaName,
    addressDetail,
    nickname,
    longitude,
    latitude
  } = options
  return fetch({
    url: 'a/v1/address',
    method: 'PUT',
    data: {
      id,
      type,
      phone,
      areaName,
      addressDetail,
      nickname,
      longitude,
      latitude
    }
  })
}

// 删除地址
export function deleteUserAddr(options) {
  const { id } = options
  return fetch({
    url: `a/v1/address/${id}`,
    method: 'delete'
  })
}

//店铺详情
export function getStoreDetail(options) {
  const { id } = options
  return fetch({
    url: `s/v1/store/${id}`,
    method: 'get'
  })
}

// 添加准订单
export function addQuasiOrder(options) {
  const { seller_id, goods, success, error } = options
  getApp().getCurrentAddress(address => {
    var data = {
      seller_id,
      goods: JSON.stringify(goods)
    }
    if (address.addr_id) {
      data = Object.assign(
        {
          addr_id: address.addr_id
        },
        data
      )
    } else {
      var location = address.location
      data = Object.assign(
        {
          city_id: address.city_id,
          city_name: address.city,
          district_id: address.district_id,
          district_name: address.district,
          longitude: location.longitude,
          latitude: location.latitude
        },
        data
      )
    }
    getApp().getLoginInfo(loginInfo => {
      if (!loginInfo.user_info) {
        return alert('用户未登录')
      }
      var { user_id, user_token } = loginInfo.user_info
      fetch({
        url: 'index.php?m=Mall&c=Order&a=addQuasiOrder',
        data: Object.assign(
          {
            user_id,
            user_token
          },
          data
        ),
        success,
        error
      })
    })
  })
}

// 获取准订单
export function getQuasiOrderInfo(options) {
  var { quasi_order_id, success, error } = options
  getApp().getLoginInfo(loginInfo => {
    if (!loginInfo.user_info) {
      return alert('用户未登录')
    }
    var { user_id, user_token } = loginInfo.user_info
    fetch({
      url: 'index.php?m=Mall&c=Order&a=getQuasiOrderInfo',
      data: {
        user_id,
        user_token,
        quasi_order_id
      },
      success,
      error
    })
  })
}

// 更新准订单地址
export function updateOrderAddr(options) {
  var { quasi_order_id, addr_id, success, error } = options
  getApp().getLoginInfo(loginInfo => {
    if (!loginInfo.user_info) {
      return alert('用户未登录')
    }
    var { user_id, user_token } = loginInfo.user_info
    fetch({
      url: 'index.php?m=Mall&c=Order&a=updateOrderAddr',
      data: {
        user_id,
        user_token,
        quasi_order_id,
        addr_id
      },
      success,
      error
    })
  })
}
// 更新准订单红包
export function updateOrderCoupon(options) {
  var { quasi_order_id, user_coupon_id, success, error } = options
  getApp().getLoginInfo(loginInfo => {
    if (!loginInfo.user_info) {
      return alert('用户未登录')
    }
    var { user_id, user_token } = loginInfo.user_info
    fetch({
      url: 'index.php?m=Mall&c=Order&a=updateOrderCoupon',
      data: {
        user_id,
        user_token,
        quasi_order_id,
        user_coupon_id
      },
      success,
      error
    })
  })
}

// 添加订单
export function addOrder(options) {
  // var { orderinfo, success, error } = options
  var { orderinfo } = options
  return fetch({
    url: 'o/v1/order',
    method: 'post',
    data: {
      ...orderinfo
    }
  })
  // getApp().getLoginInfo(loginInfo => {
  //   if (!loginInfo.token) {
  //     return alert('用户未登录')
  //   }
  //   fetch({
  //     url: 'o/v1/order',
  //     method: 'post',
  //     data: {
  //       ...orderinfo
  //     },
  //     success,
  //     error
  //   })
  // })
}

// 取消订单
export function changeOrderStatus(options) {
  var { id, bizstatus } = options
  return fetch({
    method: 'PUT',
    url: `o/v1/order/upload/${id}/${bizstatus}`
  })
}

// 获取订单列表
export function getOrders(options) {
  var { currentType, page, size } = options

  return fetch({
    url: `o/v1/${currentType}/orders?page=${page}&size=${size}`
  })
}

// 获取订单详情
export function getOrderInfo(options) {
  var { order_id } = options
  return fetch({
    url: `o/v1/order/${order_id}`
  })
}

// 订单评论
export function reviewsOrder(options) {
  var { order_id, service, quality, content, taste } = options

  return fetch({
    url: 'c/v1/comment',
    method: 'POST',
    data: {
      commentType: 1, //0仅评价，1有内容，无图，2无内容，有图，3有内容有图
      orderId: order_id,
      expressScore: service,
      decorationScore: quality,
      tasteScore: taste,
      name: content
    }
  })
}

// 获取支付参数
export function getPayment(options) {
  var { order_id, success, error } = options
  getApp().getLoginInfo(loginInfo => {
    if (!loginInfo.user_info) {
      return alert('用户未登录')
    }
    var { user_id, user_token } = loginInfo.user_info
    fetch({
      url: 'index.php?m=Mall&c=WeixinMall&a=getPayment',
      data: {
        user_id,
        user_token,
        order_id
      },
      success,
      error
    })
  })
}

// 获取分组列表
export function getSellersByCategory(options) {
  var { siteId, category_id, page, size } = options
  page = page || 0
  return fetch({
    url: `s/v1/${siteId}/${category_id}/stores`,
    data: {
      page,
      size
    }
  })
}

// 搜索商家和商品
export function search(options) {
  var { siteId, page, size, inputVal } = options
  page = page || 0
  return fetch({
    url: `s/v1/${siteId}/stores`,
    data: {
      name: inputVal,
      size: size,
      page: page
    }
  })
}

// 获取用户红包列表
export function getShareUserList(options) {
  var { page, success, error } = options
  page = page || 0
  getApp().getLoginInfo(loginInfo => {
    if (!loginInfo.user_info) {
      return alert('用户未登录')
    }
    var { user_id, user_token } = loginInfo.user_info
    fetch({
      url: 'index.php?m=Mall&c=Coupon&a=getShareUserList',
      data: {
        user_id,
        user_token,
        page
      },
      success,
      error
    })
  })
}
